package com.internship.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.internship.model.DepartmentInfo;
import com.internship.model.ProfessionInfo;
import com.internship.page.Page;
import com.internship.service.Const;
import com.internship.service.DepartmentInfoService;
import com.internship.service.ProfessionInfoService;


@Controller
@RequestMapping("profession")
public class ProfessionController {
	private final Logger logger = Logger.getLogger(ProfessionController.class);
	
	@Autowired
	private ProfessionInfoService professionInfoService;
	@Autowired
	private DepartmentInfoService 	departmentInfoService;
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}
	private void clearSession() {
		getSession().invalidate();
	}

	@RequestMapping("/profession_list.do")
	public ModelAndView getProfessionInfoList() {
		return getProfessionInfoList(null,null,1);
	}
	private List<DepartmentInfo>  departmentInfoList = null;
	@RequestMapping("/profession_page_list.do")
	public ModelAndView getProfessionInfoList(@RequestParam String SSXSXYDM,@RequestParam String ZYMC,@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("profession_info");
		HttpSession session = getSession();
		try {
			if (String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("2")||String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("1")) {
				Page<ProfessionInfo> pageList = professionInfoService.getProfessionInfoList(SSXSXYDM, ZYMC, pageNumber);
				this.departmentInfoList = departmentInfoService.getDepartmentInfoList();
				mv.addObject("currentPage", pageNumber);
				mv.addObject("SSXSXYDM", SSXSXYDM);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("departmentInfoList",departmentInfoList);
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("professionList", pageList.getResultList());
			}else {
				Page<ProfessionInfo> pageList = professionInfoService.getProfessionInfoList(String.valueOf(session.getAttribute(Const.USER_DEPARTMENT_CODE)),ZYMC, pageNumber);
				mv.addObject("currentPage", pageNumber);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("professionList", pageList.getResultList());
			}
		mv.addObject("ZYMC", ZYMC);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return mv;
	}
}